package com.biz.primus.ms.oms.service.allocation.validator;

import com.biz.primus.ms.oms.dao.po.OmsOrder;
import com.biz.primus.ms.oms.dao.po.OmsOrderItem;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
 * 商品数据验证器,主要用于确定商品是否存在,商品价格是否合法
 *
 * @author yanweijin
 * @date 2017/11/6
 */

@Component
public class ProductValidator implements OrderValidator {
	@Override
	public VerificationResult validate(OmsOrder order) {
		List<OmsOrderItem> omsOrderItems = order.getItems();
		/**
		 * todo 要求商品服务提供批量街口,接收productCodeSet,检查商品是否存在
		 *
		 */
		if(CollectionUtils.isNotEmpty(omsOrderItems)){
			for(OmsOrderItem omsOrderItem:omsOrderItems){
				Boolean finalPriceValid=omsOrderItem.getFinalPrice()==null || omsOrderItem.getFinalPrice()<=0;
				Boolean marketPriceValid=omsOrderItem.getMarketPrice()==null || omsOrderItem.getMarketPrice()<=0;
				if(finalPriceValid&&marketPriceValid){
					return new VerificationResult(true,"订单商品金额有误,无法自动分单!");
				}
			}
		}else{
			return new VerificationResult(true,"订单商品有误，无法自动分单!");
		}
		return new VerificationResult(false,null);
	}

	@Override
	public String getValidatorName() {
		return "product";
	}
}
